package com.dcivision.dms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.dcivision.dms.bean.DmsDocumentIndexCache;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;

public class DmsDocumentIndexCacheDAObject extends AbstractDAObject {
  
  public static final String TABLE_NAME = "DMS_DOCUMENT_INDEX_CACHE";
  
  public DmsDocumentIndexCacheDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("DMS_DOCUMENT_ID");
    this.vecDBColumn.add("ACTION_MODE");
    this.vecDBColumn.add("ACTION_TYPE");
    this.vecDBColumn.add("SOURCE_DOCUMENT_ID");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DMS_DOCUMENT_ID, A.ACTION_MODE, A.ACTION_TYPE, A.SOURCE_DOCUMENT_ID, ");
        sqlStat.append("A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_DOCUMENT_INDEX_CACHE A ");
        sqlStat.append("WHERE  A.ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        rs = preStat.executeQuery();
        if (rs.next()) {
          DmsDocumentIndexCache tmpDmsDocumentIndexCache = new DmsDocumentIndexCache();
          tmpDmsDocumentIndexCache.setID(getResultSetInteger(rs, "ID"));
          tmpDmsDocumentIndexCache.setDmsDocumentID(getResultSetInteger(rs, "DMS_DOCUMENT_ID"));
          tmpDmsDocumentIndexCache.setActionMode(getResultSetString(rs, "ACTION_MODE"));
          tmpDmsDocumentIndexCache.setActionType(getResultSetString(rs, "ACTION_TYPE"));
          tmpDmsDocumentIndexCache.setSourceDocumentID(getResultSetInteger(rs, "SOURCE_DOCUMENT_ID"));
          tmpDmsDocumentIndexCache.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsDocumentIndexCache.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsDocumentIndexCache.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsDocumentIndexCache.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsDocumentIndexCache.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsDocumentIndexCache.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          return(tmpDmsDocumentIndexCache);
        } else {
          return null;
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected List getList(AbstractSearchForm searchForm) throws ApplicationException {
    
    return null;
  }

  protected List getList() throws ApplicationException {
    return null;
  }
  
  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsDocumentIndexCache tmpDmsDocumentIndexCache = (DmsDocumentIndexCache) ( (DmsDocumentIndexCache) obj).clone();

    synchronized (dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO DMS_DOCUMENT_INDEX_CACHE(ID, DMS_DOCUMENT_ID, ACTION_MODE, ACTION_TYPE, SOURCE_DOCUMENT_ID, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpDmsDocumentIndexCache.getDmsDocumentID());
        setPrepareStatement(preStat, 3, tmpDmsDocumentIndexCache.getActionMode());
        setPrepareStatement(preStat, 4, (tmpDmsDocumentIndexCache.getActionType()!=null?tmpDmsDocumentIndexCache.getActionType(): null));
        setPrepareStatement(preStat, 5, tmpDmsDocumentIndexCache.getSourceDocumentID());
        setPrepareStatement(preStat, 6, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 7, new Integer(0));
        setPrepareStatement(preStat, 8, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 9, currTime);
        setPrepareStatement(preStat, 10, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 11, currTime);
        preStat.executeUpdate();
        tmpDmsDocumentIndexCache.setID(nextID);
        tmpDmsDocumentIndexCache.setRecordStatus( GlobalConstant.RECORD_STATUS_ACTIVE);
        tmpDmsDocumentIndexCache.setUpdateCount(new Integer(0));
        tmpDmsDocumentIndexCache.setCreatorID(sessionContainer.getUserRecordID());
        tmpDmsDocumentIndexCache.setCreateDate(currTime);
        tmpDmsDocumentIndexCache.setUpdaterID(sessionContainer.getUserRecordID());
        tmpDmsDocumentIndexCache.setUpdateDate(currTime);
        return (tmpDmsDocumentIndexCache);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try {
          preStat.close();
        } catch (Exception ignore) {} finally {
          preStat = null;
        }
      }
    }
  }

  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    
    return null;
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    
    return null;
  }
  
  

  protected void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    

  }

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
    
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
    
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
    
  }
  
  public List getTopList(int topLimit, String order) throws ApplicationException {
    List result = new ArrayList();
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DMS_DOCUMENT_ID, A.ACTION_MODE, A.ACTION_TYPE, A.SOURCE_DOCUMENT_ID, ");
        sqlStat.append("A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM  DMS_DOCUMENT_INDEX_CACHE A ");
        sqlStat.append("ORDER BY A."+order+" ");
        sqlStat = this.getSelectListSQL(sqlStat, 1, topLimit);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsDocumentIndexCache tmpDmsDocumentIndexCache = new DmsDocumentIndexCache();
          tmpDmsDocumentIndexCache.setID(getResultSetInteger(rs, "ID"));
          tmpDmsDocumentIndexCache.setDmsDocumentID(getResultSetInteger(rs, "DMS_DOCUMENT_ID"));
          tmpDmsDocumentIndexCache.setActionMode(getResultSetString(rs, "ACTION_MODE"));
          tmpDmsDocumentIndexCache.setActionType(getResultSetString(rs, "ACTION_TYPE"));
          tmpDmsDocumentIndexCache.setSourceDocumentID(getResultSetInteger(rs, "SOURCE_DOCUMENT_ID"));
          tmpDmsDocumentIndexCache.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsDocumentIndexCache.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsDocumentIndexCache.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsDocumentIndexCache.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsDocumentIndexCache.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsDocumentIndexCache.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          result.add(tmpDmsDocumentIndexCache);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
    return result;
  }

  public synchronized int deleteTopList(Integer topID) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    int updateCnt = 0;
    synchronized (dbConn) {
      try {
        sqlStat.append("DELETE ");
        sqlStat.append("FROM DMS_DOCUMENT_INDEX_CACHE ");
        sqlStat.append("WHERE ID<=?");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, topID);
        updateCnt = preStat.executeUpdate();
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
    return updateCnt;
  }
  
}
